package org.zfes.snowier.zbss.xzy.store.meta;

import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
@Component
public class MemoryMetaCache implements TableMetaCache {
	
	private static  Map<String,Set<String>> tableMetaContainer = Maps.newConcurrentMap();
	
	@Override
	public Map<String,Set<String>> getAllTables(){
		return tableMetaContainer;
	}
	@Override
	public  Set<String> getCols(String tablename) {
		if(!isTableExists( tablename)) {
			return Sets.newHashSet();
		}
		return tableMetaContainer.get(tablename);
	}
	@Override
	public  boolean isTableExists(String tablename) {
		return tableMetaContainer.keySet().contains(tablename);
	}
	
	@Override
	public  boolean isColExists(String tablename,String colname) {
		if(isTableExists( tablename)) {
			return tableMetaContainer.get(tablename).contains(colname);
		}
		return false;
	}
	
	
	@Override
	public  void addTable(String tablename,Set<String> cols) {
		if(!isTableExists( tablename)) {
			tableMetaContainer.put(tablename, cols);
			return;
		}
		tableMetaContainer.get(tablename).addAll(cols);
	}
	
	@Override
	public  void addTables(Map<String,Set<String>> tableMetaMap) {
		
		if(tableMetaMap==null||tableMetaMap.isEmpty()) {
			return ;
		}
		tableMetaMap.forEach((key, value)->addTable(key,value));
	}
	
	@Override
	public  void removeTable(String tablename) {
		if(isTableExists( tablename)) {
			tableMetaContainer.remove(tablename);
		}
	}
	
	@Override
	public  void addCols(String tablename,Set<String> cols) {
		addTable( tablename, cols);
	}
	
	@Override
	public  void addCol(String tablename,String col) {
		addTable( tablename, Sets.newHashSet(col));
	}
	@Override
	public  void removeCol(String tablename,String col) {
		if(!isTableExists( tablename)) {
			return;
		}
		tableMetaContainer.get(tablename).remove(col);
	}
	
}
